package controller

import (
	"project1/middleware"
	"project1/service"
	"project1/validate"

	"github.com/kataras/iris/v12"
)

type UserController struct {
	BaseController
	Service *service.UserService
}

// 构造函数
func NewUserController() *UserController {
	return &UserController{
		BaseController: *NewBaseController(),
	}
}

// 注册账号
func (l *UserController) RegisterAccount(ctx iris.Context) {
	bodyData := validate.Register{}
	err := l.ValidateParams.CheckStructParams(ctx.FormValues(), nil, &bodyData)
	if err != nil {
		l.ApiReturn(ctx, "", err)
		return
	}
	bodyData.IP = ctx.RemoteAddr()
	data, err := l.Service.Register(bodyData)
	l.ApiReturn(ctx, data, err)
}

// 账号登录函数
func (l *UserController) LoginAccount(ctx iris.Context) {
	bodyData := validate.LoginAccount{}
	err := l.ValidateParams.CheckStructParams(ctx.FormValues(), nil, &bodyData)
	if err != nil {
		l.ApiReturn(ctx, "", err)
		return
	}
	bodyData.IP = ctx.RemoteAddr()
	data, err := l.Service.Login(bodyData)
	l.ApiReturn(ctx, data, err)
}

// 修改账号参数
func (l *UserController) ModifyAccount(ctx iris.Context) {
	bodyData := validate.AccountUpdata{}
	err := l.ValidateParams.CheckStructParams(ctx.FormValues(), map[string][]string{
		"int": {"type"},
	}, &bodyData)
	if err != nil {
		l.ApiReturn(ctx, "", err)
		return
	}
	data, err := l.Service.ModifyAccount(bodyData)
	l.ApiReturn(ctx, data, err)
}

// DeleteAccount 删除账号
func (l *UserController) DeleteAccount(ctx iris.Context) {
	bodyData := validate.DeleteAccount{}
	err := l.ValidateParams.CheckStructParams(ctx.FormValues(), map[string][]string{
		"int": {"id"},
	}, &bodyData)
	if err != nil {
		l.ApiReturn(ctx, "", err)
		return
	}
	data, err := l.Service.DeleteAccount(&bodyData)
	l.ApiReturn(ctx, data, err)
}

// 查询账号列表
func (l *UserController) GetAccountList(ctx iris.Context) {
	bodyData := validate.AccountListParam{}
	err := l.ValidateParams.CheckStructParams(ctx.FormValues(), map[string][]string{
		"int": {"id", "status", "onlineStatus", "expireStatus"},
	}, &bodyData)
	if err != nil {
		l.ApiReturn(ctx, "", err)
		return
	}
	data, err := l.Service.GetAccountList(&bodyData)
	l.ApiReturn(ctx, data, err)
}

// 生成卡密
func (l *UserController) GenerateCard(ctx iris.Context) {
	bodyData := validate.GenerateCard{}
	err := l.ValidateParams.CheckStructParams(ctx.FormValues(), map[string][]string{
		"int": {"count", "validDay"},
	}, &bodyData)
	if err != nil {
		l.ApiReturn(ctx, "", err)
		return
	}
	// 获取请求账号信息
	userInfo, err := middleware.ParseToken(ctx)
	if err != nil {
		l.ApiReturn(ctx, "", err)
		return
	}
	data, err := l.Service.Carmine(&bodyData, userInfo.ID)
	l.ApiReturn(ctx, data, err)
}

// DeleteCard 批量删除卡密
func (l *UserController) DeleteCard(ctx iris.Context) {
	bodyData := validate.DeleteCards{}
	err := l.ValidateParams.CheckStructParams(ctx.FormValues(), nil, &bodyData)
	if err != nil {
		l.ApiReturn(ctx, "", err)
		return
	}
	data, err := l.Service.DeleteCarmine(&bodyData)
	l.ApiReturn(ctx, data, err)
}

// QueryCard 查询卡密
func (l *UserController) QueryCard(ctx iris.Context) {
	bodyData := validate.QueryCards{}
	err := l.ValidateParams.CheckStructParams(ctx.FormValues(), map[string][]string{
		"int": {"businessId"},
	}, &bodyData)
	if err != nil {
		l.ApiReturn(ctx, "", err)
		return
	}
	data, err := l.Service.QueryCarmine(&bodyData)
	l.ApiReturn(ctx, data, err)
}

// QueryCardList 查询卡密列表
func (l *UserController) QueryCardList(ctx iris.Context) {
	data, err := l.Service.QueryCardList()
	l.ApiReturn(ctx, data, err)
}

// UpdateCardType 修改卡密类型
func (l *UserController) UpdateCardType(ctx iris.Context) {
	bodyData := validate.UpdateCards{}
	err := l.ValidateParams.CheckStructParams(ctx.FormValues(), map[string][]string{
		"int": {"status"},
	}, &bodyData)
	if err != nil {
		l.ApiReturn(ctx, "", err)
		return
	}
	ip := ctx.RemoteAddr()
	// 获取请求账号信息
	userInfo, err := middleware.ParseToken(ctx)
	if err != nil {
		l.ApiReturn(ctx, "", err)
		return
	}
	data, err := l.Service.UpdateCardType(&bodyData, ip, userInfo.ID)
	l.ApiReturn(ctx, data, err)
}
